package com.eianiao.web;

import com.eianiao.bean.ShoppingCart;
import com.eianiao.common.R;
import com.eianiao.service.ShoppingCartService;
import com.eianiao.utils.BaseContext;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = shoppingCartService.add(shoppingCart);
        return R.success(cart);
    }


    /**
     * 获取购物车数据
     * @return
     */
    @GetMapping("/list")
    public R list(){
        List<ShoppingCart> list = shoppingCartService.findByUid(BaseContext.getCurrentId());
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(){
        int row = shoppingCartService.clean(BaseContext.getCurrentId());
        if(row >  0){
             return R.success("清空购物车成功！");
        }else{
             return R.error("清空购物车失败！");
        }
    }
}
